package com.sx.manage.controller.fund;

import java.io.IOException;

import javax.annotation.Resource;
import javax.servlet.ServletException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sx.manage.controller.base.MBaseController;
import com.sx.manage.service.fund.IMDirectRebateRuleSetService;
import com.sx.manage.servlet.ViewAndModel;
import com.sx.model.constants.MDataStatusType;
import com.sx.model.manage.mybatis.form.fund.MRebateRuleSetForm;
import com.sx.model.manage.mybatis.vo.fund.MRebateRuleSetVO;
import com.sx.model.manage.mybatis.vo.result.ResultBooleanVO;
import com.sx.model.manage.mybatis.vo.result.ResultListVO;
import com.sx.model.manage.mybatis.vo.result.ResultObjectVO;
@Controller
@RequestMapping("/mDirectRule")
public class MDirectRuleController extends MBaseController{
	
	protected Log logger = LogFactory.getLog(this.getClass());
	@Resource(name="directRuleSetServiceImpl")
	private IMDirectRebateRuleSetService directRuleSetServiceImpl;
	/**
	 * 添加界面
	 * @return
	 */
	 @RequestMapping("/toViewAdd")
	public ViewAndModel addViewRebateRule() {
		ViewAndModel model=new ViewAndModel(request,cache);
		//如需在添加页需要加载元素在此处增加  
		model.setViewName("fund/addDirectSetting");//修改ftl页面名称
		model.addObject("rebateRuleVO",new MRebateRuleSetVO());
		return model;
	}
	
	/**
	 * 保存数据
	 * @param form
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	@RequestMapping("/saveRule")
	@ResponseBody
	public ResultBooleanVO saveRule(MRebateRuleSetForm form){
		
		if(null != form.getId()){
			return this.directRuleSetServiceImpl.edit(form, loginConsumer());
		}else{
			return this.directRuleSetServiceImpl.add(form, loginConsumer());
		}
	}
	
	/**
	 * 编辑界面
	 * @param form
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	 @RequestMapping("/toViewwEdit/{id}")
	 @ResponseBody
	public ViewAndModel  toViewwEdit(MRebateRuleSetForm form){
		if(null == form ){
			return null;
		}
		 ViewAndModel model=new ViewAndModel(request,cache);
		 //查询类
		 ResultObjectVO<MRebateRuleSetVO>  result = this.directRuleSetServiceImpl.queryById( form );
		 if (null != result.getObjVo() ) {
			 model.addObject("rebateRuleVO", result.getObjVo());
		}
		 model.setViewName("fund/addDirectSetting");//修改ftl页面名称
		 return model;
	}
	
	/**
	 * 删除规则
	 * @param form
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	 @RequestMapping("/deleteRule")
	public ResultBooleanVO deleteRule(MRebateRuleSetForm form){
		if(null == form){
			return new ResultBooleanVO("m.parameter.is.null");
		}
		return this.directRuleSetServiceImpl.delete(form, loginConsumer());
	}
	 
	 /**
	  * 规则列表
	  * @return
	  */
	@RequestMapping("/ruleIndex")
	public ViewAndModel ruleIndex() {
			ViewAndModel model=new ViewAndModel(request,cache);
			//如需在添加页需要加载元素在此处增加  
			model.setViewName("fund/directIncomeSetting");//修改ftl页面名称
			return model;
		}
	
	/**
	 * 列表数据
	 * @param form
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
    @RequestMapping("/pageRule")
    @ResponseBody		 
	public ResultListVO<MRebateRuleSetVO> pageRule(MRebateRuleSetForm form){
  
		return this.directRuleSetServiceImpl.queryByForm(form);
	}
	
    /**
     * 启用
     * @return
     * @throws ServletException
     * @throws IOException
     */
    @RequestMapping("/enableRule")
    @ResponseBody
    public ResultBooleanVO enableRule(MRebateRuleSetForm form){
    	form.setData_status( MDataStatusType.DATA_AVAILABLE );
    	return directRuleSetServiceImpl.editStatus(form, loginConsumer());
    }
    
    /**
     * 停用
     * @return
     * @throws ServletException
     * @throws IOException
     */
    @RequestMapping("/disableRule")
    @ResponseBody
    public ResultBooleanVO disableRule(MRebateRuleSetForm form){
    	form.setData_status( MDataStatusType.DATA_DISABLE );
    	return directRuleSetServiceImpl.editStatus(form, loginConsumer());
    }
	

}
